<?php

namespace app\admin\controller\shop;

use library\Controller;
use think\Db;

/**
 * 店铺管理
 * Class Product
 * @package app\admin\controller\product
 */
class Shop extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'SystemShop';

    /**
     * 店铺列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '店铺管理';
        
        $where = [];       
        $where[] = ['is_deleted','eq',0]; 
        $status = $this->request->get('status'); 
        if($status){
        	$where[] = ['status','eq',$status];    
        }
        
        $query = $this->_query($this->table)->like('name');
        $query->where($where)->dateBetween('create_at,update_at')->order('sort desc,id asc')->page();
    }


    /**
     * 添加店铺
     * @auth true
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }


    /**
     * 表单数据后置处理
     * @param number $id
     * @auth true
     */
    protected function _add_form_result($id)
    {
        $info = Db::name($this->table)->where(['id' => $id])->find();
        if ($info) {
            sysoplog('店铺管理', '添加' . $info['name'] . '成功');
            $this->success('保存成功');
        }

    }

    /**
     * 编辑店铺
     * @auth true
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }


    /**
     * 表单数据后置处理
     * @param number $id
     * @auth true
     */
    protected function _edit_form_result($id)
    {
        $info = Db::name($this->table)->where(['id' => $id])->find();
        if ($info) {
            sysoplog('店铺管理', '编辑' . $info['name'] . '成功');
            $this->success('保存成功');
        }
    }


    /**
     * 禁用店铺
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '2']);
    }

    /**
     * 启用店铺
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $this->applyCsrfToken();
        $this->_save($this->table, ['status' => '1']);
    }
    
    
    /**
     * 隐藏店铺
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function hide()
    {
        $this->applyCsrfToken();
        $id = $this->request->param('id');
        $count = Db::name('system_order')->where(['shop_id'=>$id])->count();
        if($count){
        	$re = Db::name('system_order')->where(['shop_id'=>$id])->update(['is_deleted'=>1]);
	        if(!$re){
	        	$this->error('隐藏失败！');
	        }
        }       
        $this->_save($this->table, ['is_show' => '2']);
    }

    /**
     * 显示店铺
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function show()
    {
        $this->applyCsrfToken();
        $id = $this->request->param('id');
        $count = Db::name('system_order')->where(['shop_id'=>$id])->count();
        if($count){
        	$re = Db::name('system_order')->where(['shop_id'=>$id])->update(['is_deleted'=>0]);
	        if(!$re){
	        	$this->error('显示失败！');
	        }
        }     
        $this->_save($this->table, ['is_show' => '1']);
    }
    

    
}
